import api from '../../api';
import { message } from 'antd';
import { isZiGuanEvir } from '../../utils/commonFunc';

export default {
  namespace: 'etfOrderM',
  state: {},
  effects: {
    //指令下达新增
    *orderSave({ payload, callback }, { call, put }) {
      const response = yield call(api.etfOrderApi.orderSave, payload);
      if (response) {
        if (response.success) {
          callback('success');
          message.success('指令下达成功!');
        } else {
          callback(undefined);
          let msg = response.message ? response.message : response.errorMsg || '指令下达失败!';
          message.error(msg);
        }
      } else {
        callback(undefined);
        message.error('指令下达失败!');
      }
    },
    //簿记下达新增
    *tradeBookSave({ payload, callback }, { call, put }) {
      const response = yield call(api.etfOrderApi.tradeBookSave, payload);
      if (response) {
        if (response.success) {
          callback('success');
          message.success('簿记下达成功!');
        } else {
          callback(undefined);
          let msg = response.message ? response.message : response.errorMsg || '簿记下达失败!';
          message.error(msg);
        }
      } else {
        callback(undefined);
        message.error('簿记下达失败!');
      }
    },

    //查询基本信息
    *getBaseInfo({ payload, callback }, { call, put }) {
      let { tradingcode, exchangecode, maatApi } = payload;
      let args = { tradingcode: tradingcode, exchangecode: exchangecode };
      let response;
      let withMAAT = false;
      if (maatApi) {
        // 判断当前环境是不是资管
        let temp = isZiGuanEvir();
        if (!!temp && temp['iv-user']) withMAAT = true;
      }
      if (withMAAT) {
        response = yield call(api.etfOrderApi.getBaseInfoWithMAAT, args);
      } else {
        response = yield call(api.etfOrderApi.getBaseInfo, args);
      }
      if (response && response.success) {
        callback(response.data);
      } else {
        callback();
        message.error('查询基本信息失败!');
      }
    },

    //查询申赎信息
    *getStockInfo({ payload, callback }, { call, put }) {
      let { tradingcode, page, pageSize, userId, maatApi } = payload;
      let args = { tradingcode: tradingcode, page: page, pageSize: pageSize, userId: userId };
      let response;
      let withMAAT = false;
      if (maatApi) {
        // 判断当前环境是不是资管
        let temp = isZiGuanEvir();
        if (!!temp && temp['iv-user']) withMAAT = true;
      }
      if (withMAAT) {
        response = yield call(api.etfOrderApi.getStockInfoWithMAAT, args);
      } else {
        response = yield call(api.etfOrderApi.getStockInfo, args);
      }
      const columns = [
        {
          title: '证券代码',
          dataIndex: 'mtradingcode',
          key: 'mtradingcode',
          width: '50%',
        },
        {
          title: '证券数量',
          dataIndex: 'componentnum',
          key: 'componentnum',
          width: '50%',
        },
      ];
      if (response && response.success) {
        callback(response.data, columns);
      } else {
        callback();
        message.error('查询申赎信息失败!');
      }
    },

    //查询过去ETF标的当日涨停价
    *getHardenPrice({ payload, callback }, { call, put }) {
      let temp = isZiGuanEvir();
      let isZiGuan = !!temp; // 判断该页面是在资管被引入的，还是maat
      let response;
      if (isZiGuan) {
        response = yield call(api.etfOrderApi.getHardenPriceZG, payload);
      } else {
        response = yield call(api.etfOrderApi.getHardenPrice, payload);
      }

      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response.msg || '获取ETF标的当日涨停价失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
